package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.Singer;
import com.javaclimb.music.domain.Song;
import com.javaclimb.music.service.SongService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/song")
public class SongController {
    @Autowired
    private SongService songService;

    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public Object selectBySingerId(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        List<Song> list = songService.query(Integer.parseInt(request.getParameter("singerId")));
        jsonObject.put(Consts.SONGLIST, list);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteAll", method = RequestMethod.GET)
    public Object deleteAll(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("songIds");
        String[] strings = id.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(Integer.parseInt(strings[i]));
        }
        int a = songService.deleteAll(list);
        if (a > 0) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }

    @RequestMapping(value = "/selectSongName", method = RequestMethod.POST)
    public Object selectSongName(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String name = request.getParameter("name");
        Integer id = Integer.parseInt(request.getParameter("id"));
        Song song = new Song();
        song.setSingerId(id);
        song.setName(name);
        List<Song> list = songService.selectSongName(song);
        jsonObject.put(Consts.SONGLIST, list);
        return jsonObject;
    }

    @RequestMapping(value = "/insertSong", method = RequestMethod.POST)
    public Object insertSong(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        JSONObject jsonObject = new JSONObject();
        String singerId = request.getParameter("singerId");
        Integer sId = Integer.parseInt(singerId);
        String name = request.getParameter("name");
        String introduction = request.getParameter("introduction");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date createTime = null;
        Date updateTime = null;
        try {
            createTime = format.parse(request.getParameter("createTime"));
            updateTime = format.parse(request.getParameter("updateTime"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String pic = request.getParameter("pic");
        String lyric = request.getParameter("lyric");
        String fileName = file.getOriginalFilename();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateNow = sdf.format(date);
        //文件名
        String nowFileName = dateNow + fileName;
        //路径
        String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "song" + System.getProperty("file.separator") + "mp3";
        File dir = new File(path);
        //如果没有路径创建
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //实际路径
        File dest = new File(path + System.getProperty("file.separator") + nowFileName);
        String sqlDir = "/song/mp3/" + nowFileName;
        file.transferTo(dest);
        Song song = new Song();
        song.setSingerId(sId);
        song.setName(name);
        song.setIntroduction(introduction);
        song.setCreateTime(createTime);
        song.setUpdatetime(updateTime);
        song.setPic(pic);
        song.setLyric(lyric);
        song.setUrl(sqlDir);

        int isRight = songService.insertSong(song);
        if (isRight > 0) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(HttpServletRequest request) throws ParseException {
        JSONObject jsonObject = new JSONObject();
        Song song = new Song();
        Integer id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String introduction = request.getParameter("introduction");
        String data = request.getParameter("updateTime");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date updateTime = format.parse(data);
        String lyric = request.getParameter("lyric");
        song.setId(id);
        song.setName(name);
        song.setIntroduction(introduction);
        song.setUpdatetime(updateTime);
        song.setLyric(lyric);
        int a = songService.update(song);
        if (a > 0) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object delete(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        Integer id = Integer.parseInt(request.getParameter("id"));
        int a = songService.delete(id);
        if (a > 0) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }

    @RequestMapping(value = "/selectSong", method = RequestMethod.GET)
    public Object selectSong(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        Integer id = Integer.parseInt(request.getParameter("id"));
        Song song = songService.selectSong(id);
        jsonObject.put(Consts.SONG, song);
        return jsonObject;
    }

    @RequestMapping(value = "/postUpdate", method = RequestMethod.POST)
    public Object postUpdate(@RequestParam("file") MultipartFile file, @RequestParam("id") int id) throws IOException {
        String fileName = file.getOriginalFilename();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateNow = sdf.format(date);
        //文件名
        String nowFileName = dateNow + fileName;
        //路径
        String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "img" + System.getProperty("file.separator") + "songPic";
        File dir = new File(path);
        //如果没有路径创建
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //实际路径
        File dest = new File(path + System.getProperty("file.separator") + nowFileName);
        String sqlDir = "/img/songPic/" + nowFileName;
        file.transferTo(dest);
        Song song = new Song();
        song.setId(id);
        song.setPic(sqlDir);
        JSONObject jsonObject = new JSONObject();
        int isRight = songService.update(song);
        if (isRight > 0) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }


    @RequestMapping(value = "/postSong", method = RequestMethod.POST)
    public Object postSong(@RequestParam("file") MultipartFile file, @RequestParam("id") int id) throws IOException {
        String fileName = file.getOriginalFilename();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateNow = sdf.format(date);
        //文件名
        String nowFileName = dateNow + fileName;
        //路径
        String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "song" + System.getProperty("file.separator") + "mp3";
        File dir = new File(path);
        //如果没有路径创建
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //实际路径
        File dest = new File(path + System.getProperty("file.separator") + nowFileName);
        String sqlDir = "/song/mp3/" + nowFileName;
        file.transferTo(dest);
        Song song = new Song();
        song.setId(id);
        song.setUrl(sqlDir);
        JSONObject jsonObject = new JSONObject();
        int isRight = songService.update(song);
        if (isRight > 0) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }
}
